/**
as3app - An open-source ActionScript 3.0 Library developed to assist 
in creating simple visual controls/data binding/asset loading for the
Flash player platform.

http://code.google.com/p/as3app/

Copyright (c) 2011 M. K. Chowdhury, All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package as3app.visuals 
{
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	
	/**
	 * ...
	 * @author Muntasir Kabir Chowdhury
	 */
	public class ProgressBar extends Sprite
	{
		private var _width:Number;
		public function get Width():Number
		{
			this.width
			return _width;
		}
		
		
		
		
		
		
		
		
		public function set Width(value:Number) :void
		{
			_width = value;
			reDraw();
		}
		
		
		
		
		
		
		
		
		private var _height:Number;
		override public function get height():Number 
		{
			return _height;
			//return super.height;
		}
		
		
		
		
		
		
		
		
		
		override public function set height(value:Number) :void
		{
			_height = value;
			reDraw();
		}
		
		
		
		
		
		
		private var _progress:Number = 0;
		/**
		 * Set or get progress. Range : [0,1]
		 */
		public function get progress():Number { return _progress; }
		public function set progress(newProgress:Number)
		{ 
			_progress = newProgress;
			reDraw();
		}
		
		

		
		
		
		
		private var _progressSprite:DisplayObject = null;
		public function SetProgressSprite(displayObject:DisplayObject) { _progressSprite = displayObject; reDraw(true); }
		
		
		
		
		
		
		
		private var _progressStyle:Array = [0xAA2211, .9];
		public function SetProgressStyle(style:Array) { _progressStyle = style; _progressSprite = null; reDraw(true); }
		
		
		
		
		
		
		
		private var _progressBarSprite:DisplayObject = null;
		public function SetProgressBarSprite(displayObject:DisplayObject) { _progressBarSprite = displayObject; reDraw(true); }
		
		
		
		
		
		
		
		private var _progressBarStyle:Array = [0xBBEFEF, 1];
		public function SetProgressBarStyle(style:Array) { _progressBarStyle = style; _progressBarSprite = null; reDraw(true); }
		
		
		
		
		
		
		
		
		/**
		 * Creates all childrens.
		 */
		private function create()
		{
			while (numChildren > 0) this.removeChildAt(0);
			

			if (_progressSprite != null)
			{
				_progressSprite.width = _width;
				_progressSprite.height = _height;
			}
			else 
			{
				var shape:Shape = new Shape();
				shape.graphics.beginFill(_progressStyle[0], _progressStyle[1]);
				shape.graphics.drawRect(0, 0, _width, _height);
				shape.graphics.endFill();
				
				_progressSprite = shape as DisplayObject;
			}
			
			if (_progressBarSprite != null)
			{
				_progressBarSprite.width = _width;
				_progressBarSprite.height = _height;
			}
			else 
			{
				var shape:Shape = new Shape();
				shape.graphics.beginFill(_progressBarStyle[0], _progressBarStyle[1]);
				shape.graphics.drawRect(0, 0, 1, 1);
				shape.graphics.endFill();
				
				_progressBarSprite = shape as DisplayObject;
			}
			
			addChild(_progressBarSprite);
			addChild(_progressSprite);
		}
		
		
		
		
		
		
		/**
		 * Redraw control
		 * @param	createRequired
		 */
		private function reDraw(createRequired:Boolean=false)
		{
			if (createRequired) create();
			
			_progressBarSprite.width = _width;
			_progressBarSprite.height = _height;
			
			_progressSprite.width = _progress == 0 ?1:(_progress > 0 && _progress <= 1?_width * _progress:1);
			_progressSprite.height = _height;
		}
		
		
		
		
		
		
		/**
		 * Creates ProgressBar instance
		 * @param	pWidth
		 * @param	pHeight
		 * @param	progressSprite
		 * @param	progressBarSprite
		 */
		public function ProgressBar(pWidth:Number = 300, pHeight:Number = 50, progressSprite:DisplayObject = null, progressBarSprite:DisplayObject = null ) 
		{
			_width = pWidth;
			_height = pHeight;
			_progress = 0;
			_progressSprite = progressSprite;
			_progressBarSprite = progressBarSprite;
			
			create();
		}
		
		
		
		
		/**
		 * Reset progress to default.
		 */
		public function reset() { progress = 0; }
		
		
	}

}